<?php
header("content-type:text/html;charset=utf-8");
?>
<html lang="zh-CN">
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
	<title>我的工作 -- PHPExcel</title>
	
	<!-- Bootstrap -->
	<link href="/public/css/bootstrap.min.css" rel="stylesheet">
	<link rel="stylesheet" type="text/css" href="/public/css/main.css"/>
	
	<!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询（media queries）功能 -->
	<!-- 警告：通过 file:// 协议（就是直接将 html 页面拖拽到浏览器中）访问页面时 Respond.js 不起作用 -->
	<!--[if lt IE 9]>
	<script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
	<script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
	<![endif]-->
</head>
<body><br />
<div class="container">
	<ul class="nav nav-pills">
	    <li><a href="index_read.php">PHPExcel读取</a></li>
	   	<li><a class="btn btn-default" href="index_write.php">PHPExcel写入</a></li>
	</ul>
	<hr />
	<div class="tab-content min_hei">
	    <!--PHPExcel写入-->
	    <div class="tab-pane active" id="write">
	    	<h4>PHPExcel写入：把数据库里的数据写入到Excel，也就是数据导出（从MySQL数据库导出到Excel）</h4>
	    	<h4>数据库里默认有少量数据，可以做一键导出，体验数据导出操作，也可以添加几条数据后再导出，查看导出效果</h4>

<?php
error_reporting(0);
include "../../public/config/config.php";
include "../../public/include/funcs.php";
$link=conn($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd,$cfg_dbname,$cfg_dbchar);
$arr=select($link, 'excel');
$num = count($arr);
echo '数据库共有 '.$num.' 条数据记录';
$sick_name = $_POST['sick_name'];
if($sick_name){
	$addArr['name'] = $sick_name;
	$addArr['sex'] = $_POST['sex'] ? '男' : '女';
	$addArr['age'] = $_POST['age'];
	$addArr['tel'] = $_POST['tel'];
	$re = add($link, 'excel', $addArr);
	if($re){
		echo "<script>";
		echo "alert('记录添加成功！');";
		echo "window.location.href='index_write.php';";
		echo "</script>";
	}
}
if($_GET['dataOut'] == "dataOut"){

/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('PRC');


/** 引入PHPExcel */
require_once dirname(__FILE__) . './Classes/PHPExcel.php';

// 创建Excel文件对象
$objPHPExcel = new PHPExcel();
// 设置文档信息，这个文档信息windows系统可以右键文件属性查看
$objPHPExcel->getProperties()->setCreator("作者简庆旺")
    ->setLastModifiedBy("最后更改者")
    ->setTitle("文档标题")
    ->setSubject("文档主题")
    ->setDescription("文档的描述信息")
    ->setKeywords("设置文档关键词")
    ->setCategory("设置文档的分类");

$zmStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
//echo $zmStr{3};
$dataArr[] = ['ID','姓名','性别','年龄','电话'];
foreach($arr as $v){
	$dataArr[] = array_values($v);
}
//echo "<pre>";
//print_r($dataArr);
//echo "</pre>";
foreach($dataArr as $k=>$v){
	foreach($v as $kk=>$vv){
		$objPHPExcel->setActiveSheetIndex(0)->setCellValue($zmStr{$kk}.($k+1), $vv);
	}
}

//根据$dataArr数据，添加数据
//$objPHPExcel->setActiveSheetIndex(0)->setCellValue('D2', '世界1');


// 重命名工作sheet
$objPHPExcel->getActiveSheet()->setTitle('第一个sheet');

// 设置第一个sheet为工作的sheet
$objPHPExcel->setActiveSheetIndex(0);

// 保存Excel 2007格式文件，保存路径为当前路径，名字为export.xlsx
//$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
//$objWriter->save( 'test_write.xlsx');


// 保存Excel 95格式文件，，保存路径为当前路径，
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('excel_write.xls');
}

?>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="index_write.php?dataOut=dataOut" class="btn btn-info">导出</a>


	    	<h3 class="bg-primary h3_title text_center">添加一条记录</h3>
			<form action="#" method="post" class="form-horizontal form_border" name="frm">
				<div class="form-group">
					<label for="sick_name" class="col-sm-2 control-label">姓名：</label>
					<div class="col-sm-10 diy_font">
						<input type="text" class="form-control w300 inline" name="sick_name" id="sick_name" value="" /> &nbsp;&nbsp;&nbsp;必填
					</div>
				</div>
				
				<div class="form-group">
					<label for="sex" class="col-sm-2 control-label">性别：</label>
					<div class="col-sm-10 diy_font">
						<label class="radio-inline">
							<input type="radio" name="sex" value="1" checked/>男
						</label>
						<label class="radio-inline">
							<input type="radio" name="sex" value="0" />女
						</label>
					</div>
				</div>
				<div class="form-group">
					<label for="age" class="col-sm-2 control-label">年龄：</label>
					<div class="col-sm-10 diy_font">
						<input type="text" class="form-control w300 inline" name="age" id="age" value="" />
					</div>
				</div>
				<div class="form-group">
					<label for="tel" class="col-sm-2 control-label">电话：</label>
					<div class="col-sm-10 diy_font">
						<input type="text" class="form-control w300 inline" name="tel" id="tel" value="" /> &nbsp;&nbsp;&nbsp;没有做验证，随意填写一串数字
					</div>
				</div>
				<div>
					<div class="col-sm-2"></div>
					<div class="col-sm-10"><input class="btn btn-info" type="submit" value="提交"/></div>
					<div class="clear"></div>
				</div>
			</form>
	    	
	    </div>
	    
	</div>
</div>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
<script type="text/javascript" src="/public/js/jquery-3.3.1.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script type="text/javascript" src="/public/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/public/js/footer.js"></script>
</body>
</html>
<?php
if($_GET['dataOut'] == "dataOut"){
	echo "<script>";
	echo "alert('导出到Excel成功！');";
	echo "window.location.href='excel_write.xls';";
	echo "</script>";
}
?>